package com.cn.yunhe.rentcars.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.CarOrderMapper;
import com.cn.yunhe.rentcars.model.CarOrder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 
 * @author 李亚磊
 *	我要租车模块：租车登记
 */

@RestController
public class PayOrdersController {
	/**
	 * 注入CarOrderMapper接口
	 */
	@Autowired
	private CarOrderMapper carOrderMapper;
	
	/**
	 * 注入CarMapper接口
	 */
	@Autowired
	private CarMapper carMapper;
	
	/**
	 * 注入日志接口
	 */
	@Autowired
	private BusiLogMapper busiLogMapper;
	
	/**
	 * 支付订单左侧跳转页面
	 */
	@RequestMapping(value="/payorders", method=RequestMethod.GET)
	public ModelAndView payCarOrders(){
		return new ModelAndView("/payorders");
	}
	
	/**
	 * 查询订单表所有状态为待支付1的的订单
	 */
	@RequestMapping(value="/allorders", method=RequestMethod.POST)
    public Map<String,Object> getAllOrders(@RequestParam(required=false) String mobile,@RequestParam(required=false) String orderid,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
	    int pageNum = (start/length)+1;//获取当前查询第几页
        int pageSize = length;
        PageHelper.startPage(pageNum, pageSize);
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("mobile", mobile);
        paramsMap.put("orderid", orderid);
        paramsMap.put("orderstate", "1");
        List<CarOrder> carOrderList=carOrderMapper.getAllOrders(paramsMap);
        PageInfo<CarOrder> page=new PageInfo<CarOrder>(carOrderList);
        
        Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("draw", draw);
        rsMap.put("data", page.getList());        
        rsMap.put("recordsTotal", page.getTotal());
        rsMap.put("recordsFiltered", page.getTotal());
        return rsMap;
    }
	
	/**
	 * 删除表单中的一条信息
	 * 将对应的车辆状态改为6，待租赁
	 * @param delCarOrder
	 */
	@RequestMapping(value="/delCarOrder", method=RequestMethod.POST)
    public void delCarOrder(@RequestParam String orderid,HttpServletRequest request){
	    //TODO:将删除订单对应的车辆状态改为6，待租赁
	    String carid = carOrderMapper.getOrdersById(orderid).getCarid();
	    Map<String, String> paramsMap = new HashMap<String, String>();
	    paramsMap.put("carid", carid);
	    paramsMap.put("carstate", "6");
	    carMapper.updateCarState(paramsMap);
		//日志插入
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "删除预订单将对应的车辆状态改为6，待租赁");
		busMap.put("busitype", "修改");
		busMap.put("busiconent","删除预订单将对应的车辆状态改为6，待租赁");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		
	    carOrderMapper.delCarOrder(orderid);
		//日志插入
		Map<String,Object> busMap2 = new HashMap<String,Object>();
		busMap2.put("logid", UUID.randomUUID().toString());
		busMap2.put("businame", "删除预订单");
		busMap2.put("busitype", "删除");
		busMap2.put("busiconent","删除预订单");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap2);	   
    }
	
	/**
	 * 批量删除
	 * 同时将对应 的车辆状态改为6待租赁
	 */
	@RequestMapping(value="/delOrders", method=RequestMethod.POST)
	public void delOrders(@RequestParam String orderid,HttpServletRequest request){
	    System.out.println("orderid------"+orderid);
	    List<String> orderidList = Arrays.asList(orderid.split(","));
	    for(String a:orderidList){
	    	//TODO:将删除订单对应的车辆状态改为6，待租赁
		    String carid = carOrderMapper.getOrdersById(a).getCarid();
		    Map<String, String> paramsMap = new HashMap<String, String>();
		    paramsMap.put("carid", carid);
		    paramsMap.put("carstate", "6");
		    carMapper.updateCarState(paramsMap);
		    //日志插入
			Map<String,Object> busMap = new HashMap<String,Object>();
			busMap.put("logid", UUID.randomUUID().toString());
			busMap.put("businame", "删除预订单将对应的车辆状态改为6，待租赁");
			busMap.put("busitype", "修改");
			busMap.put("busiconent","删除预订单将对应的车辆状态改为6，待租赁");
			BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	    }
	    carOrderMapper.delCarOrders(orderidList);
	    	//日志插入
	  		Map<String,Object> busMap2 = new HashMap<String,Object>();
	  		busMap2.put("logid", UUID.randomUUID().toString());
	  		busMap2.put("businame", "批量删除预订单");
	  		busMap2.put("busitype", "批量删除");
	  		busMap2.put("busiconent","批量删除预订单");
	  		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap2);
	}
	
	/**
	 * 跳转到确认支付页面
	 * @param orderid
	 * @throws ParseException 
	 */
	@RequestMapping(value="/confirm-pay", method=RequestMethod.GET)
	public ModelAndView getCarOrderInfo(@RequestParam String mobile,@RequestParam String orderid) throws ParseException{
	    //TODO:先到部门表查询具体位置
	    String catchAddress = carOrderMapper.getBtrentdeptAddress(orderid);
	    String backAddress = carOrderMapper.getEtrentdeptAddress(orderid);
	    //TODO:到订单表查询订单的信息
	    CarOrder carOrder = carOrderMapper.getCarOrderInfo(orderid);
	    carOrder.setCatchAddress(catchAddress);
	    carOrder.setBackAddress(backAddress);
	    
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date d1 = sdf.parse(carOrder.getReadybttime());
	    Date d2 = sdf.parse(carOrder.getReadettime());
	    long day = d2.getTime()-d1.getTime();
	    long hour = day/((60*60)*1000);
	    String rentday="";
	    if(hour%24==0){
	        rentday+=(hour/24)+"天";
	    }
	    else{
	        rentday+=(hour/24)+"天"+(hour%24)+"小时";
	    }
	    Map<String,Object> rs = new HashMap<String,Object>();
	    rs.put("carOrder", carOrder);
	    rs.put("rentday",rentday);
	    rs.put("mobile",mobile);
//	    model.addAttribute(carOrder);
	    return new ModelAndView("confirmpay",rs);
	   
	}
	/**
	 * 从确定缴费跳转到最后支付页面
	 * @param orderid
	 * realrent 实收租赁费用
	 * realbasic实收基本保险
	 * realbttime实际取车时间
	 * realtotal实收总额
	 */
	@RequestMapping(value="/paylast", method=RequestMethod.POST)
	public ModelAndView getPayWay(@RequestParam String mobile,@RequestParam String orderid,
			@RequestParam String realrent,@RequestParam String realbasic,@RequestParam String realbttime,
			@RequestParam String realtotal){
		//TODO:将这些字段更新到订单表中
		System.out.println(realbttime);
		Map<String,String> rs1 = new HashMap<String,String>();
		rs1.put("realrent", realrent);
		rs1.put("realbasic", realbasic);
		rs1.put("realbttime", realbttime);
		rs1.put("realtotal", realtotal);
		rs1.put("orderid", orderid);
		carOrderMapper.updateRealPay(rs1);
		
	    CarOrder carOrder = carOrderMapper.getCarOrderInfo(orderid);
	    Map<String,Object> rs = new HashMap<String,Object>();
        rs.put("carOrder", carOrder);
        rs.put("mobile", mobile);
        rs.put("realtotal", realtotal);
        return new ModelAndView("pay",rs);
	}
	
	/**
	 * 支付完成，更新订单状态
	 */
	@RequestMapping(value="/updatepay", method=RequestMethod.POST)
	public void updatePayOrder(@RequestParam String orderid,@RequestParam int payway,
			HttpServletRequest request){
	    Map<String,Object> paramsMap = new HashMap<String,Object>();
	    paramsMap.put("orderid", orderid);
	    paramsMap.put("payway", payway);
	    paramsMap.put("orderstate", "3");
	    carOrderMapper.updatePayOrder(paramsMap);
	    //日志插入
		Map<String,Object> busMap = new HashMap<String,Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "支付完成，更新订单状态3已支付");
		busMap.put("busitype", "更新");
		busMap.put("busiconent","支付完成，更新订单状态3已支付");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);	    
	}
}
